const defaultState = {
  loading: false,
  error: false,
  articleList: []
};

export const LOAD_ARTICLES = 'load_articles';
export const LOAD_ARTICLES_SUCCESS = 'load_articles_success';
export const LOAD_ARTICLES_ERROR = 'load_articles_error';

export const loadArticles = () => {
  console.log('触发action loadArticles');
  return {
    types: [LOAD_ARTICLES, LOAD_ARTICLES_SUCCESS, LOAD_ARTICLES_ERROR],
    url: '/api/articles'
  }
};

export default (state = defaultState, action) => {
  switch (action.type) {
    case LOAD_ARTICLES: {
      return {
        ...state,
        loading: true,
        error: false,
      };
    }

    case LOAD_ARTICLES_SUCCESS: {
      return {
        ...state,
        loading: false,
        error: false,
        articleList: action.payload
      };
    }

    case LOAD_ARTICLES_ERROR:
      return {
        ...state,
        loading: false,
        error: true
      };
    default:
      return state;
  }
}